package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.ComplainActivity;
import com.ln.lnzw.activity.EvaluationActivity;
import com.ln.lnzw.activity.MyConsultationActivity;
import com.ln.lnzw.activity.SuggestionActivity;
import com.ln.lnzw.activity.LoginActivity;
import com.ln.lnzw.activity.MaterialDirListActivity;
import com.ln.lnzw.activity.MineCollectionActivity;
import com.ln.lnzw.activity.OfficeRecordsActivity;
import com.ln.lnzw.activity.RegisteredActivity;
import com.ln.lnzw.activity.SettingsActivity;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.utils.ToastFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by wuwei on 2018/3/21.
 * 我的
 */

public class MineFragment extends BaseFragment {

    @BindView(R.id.person)
    CircleImageView person;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.renzheng)
    TextView renzheng;
    @BindView(R.id.ll_my_banjianjilu)
    LinearLayout llMyBanjianjilu;
    @BindView(R.id.ll_my_xiaoxi)
    LinearLayout llMyXiaoxi;
    @BindView(R.id.ll_my_zhifu)
    LinearLayout llMyZhifu;
    @BindView(R.id.ll_my_shoucang)
    LinearLayout llMyShoucang;
    @BindView(R.id.rl_my_cailiao)
    RelativeLayout rlMyCailiao;
    @BindView(R.id.rl_my_kuaidi)
    RelativeLayout rlMyKuaidi;
    @BindView(R.id.rl_my_pingjia)
    RelativeLayout rlMyPingjia;
    @BindView(R.id.rl_my_jianyi)
    RelativeLayout rlMyJianyi;
    @BindView(R.id.rl_my_tousu)
    RelativeLayout rlMyTousu;
    @BindView(R.id.rl_my_set)
    RelativeLayout rlMySet;
    @BindView(R.id.rl_my_zixun)
    RelativeLayout rlMyZixun;
    Unbinder unbinder;
    @BindView(R.id.zhuce)
    TextView zhuce;
    @BindView(R.id.denglu)
    TextView denglu;
    @BindView(R.id.rl_name_certified)//认证状态
    RelativeLayout rlNameCertified;
    @BindView(R.id.rl_login_regist)//登录and注册
    RelativeLayout rlLoginRegist;
    private Bundle d;

    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.minefragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        if (getIsLogin()){
            loginIsView();
        }else{
            rlLoginRegist.setVisibility(View.VISIBLE);
//            name.setText(activity.spUtils.getString(AppConstant.USER_NAME));
            rlNameCertified.setVisibility(View.GONE);
            person.setImageResource(R.mipmap.icon_register_defult_head);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i("666", "onViewCreated: ");
        EventBus.getDefault().register(this);

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_my_banjianjilu, R.id.ll_my_xiaoxi, R.id.ll_my_zhifu, R.id.ll_my_shoucang, R.id.rl_my_cailiao, R.id.rl_my_kuaidi, R.id.rl_my_pingjia, R.id.rl_my_jianyi, R.id.rl_my_tousu, R.id.rl_my_set, /*R.id.zhuce, R.id.denglu,*/R.id.rl_login_regist,R.id.rl_my_zixun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_my_banjianjilu:
                if (getIsLogin()){
                    ActivityUtils.startActivity(activity, OfficeRecordsActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.ll_my_xiaoxi:
                if (getIsLogin()){
                    Bundle bundleXiaoxi = new Bundle();
                    bundleXiaoxi.putString("name","我的消息");
                    ActivityUtils.startActivity(bundleXiaoxi,activity, MineCollectionActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.ll_my_zhifu:
                if (getIsLogin()){
                    Bundle bundleZhifu = new Bundle();
                    bundleZhifu.putString("name","我的支付");
                    ActivityUtils.startActivity(bundleZhifu,activity, MineCollectionActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.ll_my_shoucang:
                if (getIsLogin()){
                    Bundle bundle = new Bundle();
                    bundle.putString("name","我的收藏");
                    ActivityUtils.startActivity(bundle,activity, MineCollectionActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_cailiao:
                if (getIsLogin()){
                    Bundle bundleCailiao = new Bundle();
                    bundleCailiao.putString("name","我的材料库");
                    ActivityUtils.startActivity(bundleCailiao,activity, MaterialDirListActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_kuaidi:
                if (getIsLogin()){
                    Bundle bundleKuaidi = new Bundle();
                    bundleKuaidi.putString("name","我的快递");
                    ActivityUtils.startActivity(bundleKuaidi,activity, MineCollectionActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_pingjia:
                if (getIsLogin()){
                    d = new Bundle();
                    d.putString("name", "我的评价");
                    ActivityUtils.startActivity(d, activity, EvaluationActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_jianyi:
                if (getIsLogin()){
                    d = new Bundle();
                    d.putString("name", "我的建议");
                    ActivityUtils.startActivity(d, activity, SuggestionActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_zixun:
                if (getIsLogin()){
                    d = new Bundle();
                    d.putString("name", "我的咨询");
                    ActivityUtils.startActivity(d, activity, MyConsultationActivity.class);
                }else {
                    toLogin();
                }
                break;
            case R.id.rl_my_tousu:
                if (getIsLogin()){
                    d = new Bundle();
                    d.putString("name", "我的投诉");
                    ActivityUtils.startActivity(d, activity, ComplainActivity.class);
                }else {
                    toLogin();
                }
                break;
//            case R.id.denglu:
//                ActivityUtils.startActivity(activity, LoginActivity.class);
//                break;
            case R.id.rl_login_regist:
                ActivityUtils.startActivity(activity, LoginActivity.class);
                break;
//            case R.id.zhuce:
//                ActivityUtils.startActivity(activity, RegisteredActivity.class);
//                break;
            case R.id.rl_my_set:
                ActivityUtils.startActivity(activity, SettingsActivity.class);
                break;
        }
    }


    @OnClick()
    public void onViewClicked() {
    }

    @Subscribe
    public void onEvent(String isLogin) {
        if ("login".equals(isLogin)) {
            loginIsView();
        }
        if ("change".equals(isLogin)){
            rlLoginRegist.setVisibility(View.VISIBLE);
//            name.setText(activity.spUtils.getString(AppConstant.USER_NAME));
            rlNameCertified.setVisibility(View.GONE);
            person.setImageResource(R.mipmap.icon_register_defult_head);
        }
    }
    //登录状态下，登录页面view的显示隐藏，以及赋值
    private void loginIsView() {
        rlLoginRegist.setVisibility(View.GONE);
        rlNameCertified.setVisibility(View.VISIBLE);
        name.setText(activity.spUtils.getString(AppConstant.TRUE_NAME));
        Log.e("Pers", "loginIsView: "+activity.spUtils.getString(AppConstant.HEADURL) );
        Glide.with(activity).load(activity.spUtils.getString(AppConstant.HEADURL)).placeholder(R.mipmap.icon_register_defult_head).into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                person.setImageDrawable(resource);
            }
        });
        renzheng.setText(activity.spUtils.getString(AppConstant.USER_TRUE_NAME_STATE,"未实名认证"));
    }

    /**
     * 判断是否已经登录,用token做依据
     * @return 用户是否已经登录
     */
    private boolean getIsLogin(){
        return !activity.spUtils.getString(AppConstant.TOKEN).isEmpty();
    }

    /**
     * 跳转登录页面,当用户未登录的时候跳转到登录页的方法
     */
    private void toLogin(){
        ActivityUtils.startActivity(activity, LoginActivity.class);
    }
}
